import { createSlice } from "@reduxjs/toolkit";
import { User } from "@/modules/login";
export interface UserState {
  user: User | null;
}

const initialState: UserState = {
  user: null,
};

export const userSlice = createSlice({
  name: "user",
  initialState,
  reducers: {
    setUser: (state, data) => {
      state.user = data.payload;
      window.localStorage.setItem("user", JSON.stringify(state.user));
    },
    delUser: (state) => {
      state.user = null;
      window.localStorage.removeItem("user");
    },
  },
});

export const { setUser, delUser } = userSlice.actions;

export default userSlice.reducer;
